package com.tengju.user.infrastructure.repository.sign;

import com.tengju.user.domain.model.archive.IdCardNo;
import com.tengju.user.domain.model.archive.resume.BankCard;
import com.tengju.user.domain.model.archive.resume.BankCardId;
import com.tengju.user.domain.model.archive.resume.BankCardRepository;
import com.tengju.user.domain.model.archive.resume.BankCardStatus;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.AccountId;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.BankCardDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.generate.BankCardDOMapper;
import com.tengju.user.infrastructure.repository.convert.BankCardConvert;
import org.springframework.stereotype.Repository;

@Repository
public class BankCardRepositoryImpl implements BankCardRepository {

    private final BankCardDOMapper bankCardDOMapper;

    public BankCardRepositoryImpl(BankCardDOMapper bankCardDOMapper) {
        this.bankCardDOMapper = bankCardDOMapper;
    }

    @Override
    public BankCard findLastOne(UserIdCode idCode, BankCardStatus status) {
        BankCardDO bankCardDO = bankCardDOMapper.findLastOne(idCode.getValue(), status.getVal());
        return BankCardConvert.convert(bankCardDO);
    }

    @Override
    public BankCard find(UserIdCode idCode, AccountId accountId) {
        BankCardDO bankCardDO = bankCardDOMapper
                .selectByIdCodeAndAccountId(idCode.getValue(), accountId.getValue(),
                        BankCardStatus.ENABLE.getVal());
        return BankCardConvert.convert(bankCardDO);
    }

    @Override
    public BankCard find(UserIdCode idCode, BankCardStatus status, AccountId accountId) {
        BankCardDO bankCardDO = bankCardDOMapper
                .selectByIdCode(idCode.getValue(), status.getVal(), accountId.getValue());
        return BankCardConvert.convert(bankCardDO);
    }

    @Override
    public BankCard find(BankCardId bankCardId, BankCardStatus status, AccountId accountId) {
        BankCardDO bankCardDO = bankCardDOMapper
                .selectByIdAndAccountId(bankCardId.getValue(), status.getVal(), accountId.getValue());
        return BankCardConvert.convert(bankCardDO);
    }

    @Override
    public int update(BankCardId bankCardId, BankCard bankCard) {
        BankCardDO record = BankCardConvert.convert(bankCardId, bankCard);
        return bankCardDOMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int update(UserIdCode idCode, BankCardStatus status) {
        return bankCardDOMapper.updateStatusByIdCode(idCode.getValue(), status.getVal());
    }

    @Override
    public int update(UserIdCode idCode, BankCardStatus current, BankCardStatus update) {
        return bankCardDOMapper.updateStatusByIdCodeAndStatus(idCode.getValue(), current.getVal(), update.getVal());
    }

    @Override
    public int save(BankCard bankCard) {
        BankCardDO record = BankCardConvert.convert(bankCard);
        return bankCardDOMapper.insertSelective(record);
    }

    @Override
    public BankCard find(IdCardNo idCardNo, BankCardStatus status) {
        BankCardDO bankCardDO = bankCardDOMapper.selectByIdCard(idCardNo.getValue(), status.getVal());
        return BankCardConvert.convert(bankCardDO);
    }

    @Override
    public int count(UserIdCode userIdCode, IdCardNo idCardNo, BankCardStatus status) {
        return bankCardDOMapper.count(userIdCode.getValue(), idCardNo.getValue(), status.getVal());
    }
}
